package com.glsc.ngateway.common.base.repo.mysql.cmdb;

import com.glsc.ngateway.common.base.domain.mysql.cmdb.CmdbPartStandard;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface CmdbPartStandardRepository extends JpaRepository<CmdbPartStandard, Integer>, JpaSpecificationExecutor<CmdbPartStandard> {
    List<CmdbPartStandard> findAllByIdIn(List<Long> id);

    @Query(value = "select m from CmdbPartStandard m where m.isDelete = 0 " +
            "and (:#{#paramSearchDto.partStandardCode} is null OR m.partStandardCode LIKE CONCAT('%', :#{#paramSearchDto.partStandardCode}, '%')) " +
            "and (:#{#paramSearchDto.name} is null OR m.name LIKE CONCAT('%', :#{#paramSearchDto.name}, '%')) " +
            "and (:#{#paramSearchDto.brand} is null OR m.brand = :#{#paramSearchDto.brand}) " +
            "and (:#{#paramSearchDto.partNum} is null OR m.partNum LIKE CONCAT('%',:#{#paramSearchDto.partNum}, '%')) " +
           "and (:#{#paramSearchDto.standardNum} is null or m.standardNum LIKE CONCAT('%', :#{#paramSearchDto.standardNum}, '%')) " +
            "and (:#{#paramSearchDto.adaptModel} is null OR m.adaptModel = :#{#paramSearchDto.adaptModel}) " +
            "and (:#{#paramSearchDto.performance} is null OR m.performance = :#{#paramSearchDto.performance}) " +
            "and (:#{#paramSearchDto.channelAmount} is null OR m.channelAmount = :#{#paramSearchDto.channelAmount}) " +
            "and (:#{#paramSearchDto.portType} is null OR m.portType = :#{#paramSearchDto.portType}) " +
            "and (:#{#paramSearchDto.partTypeId} is null OR m.partTypeId = :#{#paramSearchDto.partTypeId}) " +
            "and (:#{#paramSearchDto.reserve1} is null OR m.reserve1 = :#{#paramSearchDto.reserve1}) " +
            "and (:#{#paramSearchDto.reserve2} is null OR m.reserve2 = :#{#paramSearchDto.reserve2}) ")
    Page<CmdbPartStandard> searchByParam(Pageable pageable, CmdbPartStandard paramSearchDto);

    @Transactional
    @Modifying
    @Query(value = "update t_cmdb_part_standard set is_delete = :deleted where part_standard_code in :codes ", nativeQuery = true)
    void updateIsDeleteByCodes(Integer deleted, List<String> codes);

    CmdbPartStandard findOneByPartStandardCode(String partStandardCode);

    List<CmdbPartStandard> findAllByNameAndIsDelete(String name, Integer noDelete);

    List<CmdbPartStandard> findAllByPartNumAndIsDelete(String partNum, Integer noDelete);
}

